// pages/index/login/login.js
const MD5=require("../js/MD5")
Page({

  /**
   * 页面的初始数据
   */
  data: {
color:"display",
loginDialog:true,
phone:"",
password:"",
states:"",
buttom:200,
merchant:"",
statusCode:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //调用弹窗组件
    this.dialog = this.selectComponent("#dialog");
  },
 
  userInput: function (e) {
    this.setData({
      phone:e.detail.value
   })  
   
  },
  passInput: function (e) {
    this.setData({
      password:e.detail.value
   })  
  //  console.log(MD5.md5(e.detail.value))
   
  },
  register:function(){
    let that=this
if (that.data.color=="hide") {
  this.setData({
    color:"display"
  })
}else{
  this.setData({
    color:"hide"
  })
}
wx.navigateTo({
  url: '/pages/index/register/register',
})
  },
    //确认事件 
 _success() {
      this.dialog.hidedialog();
      this.setData({
        loginDialog:true,
      })
    },

login:function(e){
  let that=this
  let phone= that.data.phone
  let password= that.data.password
if(!(/^1[3456789]\d{9}$/.test(phone))){ 
this.dialog.showdialog();
this.setData({
  loginDialog:false,
  state:"手机号码有误，请重填"
})
} else if(password!="")
{
wx.request({
  // url: 'http://112.124.45.35: 8090/w/person-info' ,
  url: 'http://192.168.19.114:8080/blm_war_exploded/userLogin',
  // url:"http://www.violetevergarden.cf:8888/user/login",

  method:"POST",
 data:{
  uMobile:that.data.phone,
  uPassword:password,
 },
 success (res) {
  console.log(res)
  //登录状态
  wx.setStorage({//获取用户id存入
    data: res.cookies[0].userid,
    key: 'userid',
  })
  wx.setStorage({//授权获取token，
    data: res.header.SetCookie,
    key: 'session',
  })
  wx.setStorage({//授权获取token，
    data: res.cookies,
    key: 'sessionid',
  })

    // that.data.merchant=res.data.merchant
    that.data.status=res.data.status
// console.log(wx.getStorage({
//   key: 'sessionid',
//   key: 'session',
 
// }))
  // console.log(that.data.status)
  if (that.data.status=="1") {

    that.dialog.showdialog();
   that.setData({
     loginDialog:false,
     state:"登录成功"
   })
   wx.switchTab({
    url:"/pages/home/home",
  })
 
 } else if(that.data.status=="0") {
  that.dialog.showdialog();
   that.setData({
     loginDialog:false,
     state:"账号或密码错误"
   })
 }else{
   that.dialog.showdialog();
   that.setData({
     loginDialog:false,
     state:"账号未注册，请注册后再登录"
   })
 }
 
},

})


//判断登录回调


}else{
  this.dialog.showdialog();
  this.setData({
    loginDialog:false,
    state:"密码不能为空"
  })
}


 },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})